- name: verify koschei frontend
  hosts: "{{ koschei_web_hosts |  default('koschei-web:koschei-web-stg') }}"
  vars_files:
  - /srv/web/infra/ansible/vars/global.yml
  - /srv/private/ansible/vars.yml
  - /srv/web/infra/ansible/vars/{{ ansible_distribution }}.yml
  handlers:
  - import_tasks: "{{ handlers_path }}/restart_services.yml"
  roles:
  - mod_wsgi
  - koschei/frontend

- name: verify koschei backend
  hosts: "{{ koschei_backend_hosts |  default('koschei-backend:koschei-backend-stg') }}"
  vars_files:
  - /srv/web/infra/ansible/vars/global.yml
  - /srv/private/ansible/vars.yml
  - /srv/web/infra/ansible/vars/{{ ansible_distribution }}.yml
  handlers:
  - import_tasks: "{{ handlers_path }}/restart_services.yml"
  roles:
  - koschei/backend

- name: upgrade koschei backend
  hosts: "{{ koschei_backend_hosts |  default('koschei-backend:koschei-backend-stg') }}"
  vars_files:
  - /srv/web/infra/ansible/vars/global.yml
  - /srv/private/ansible/vars.yml
  - /srv/web/infra/ansible/vars/{{ ansible_distribution }}.yml
  vars:
    fedora_repos:
    - updates
  pre_tasks:
  - name: schedule nagios downtime
    nagios: action=downtime minutes=20 service=host host={{ inventory_hostname_short }}{{ env_suffix }}
    delegate_to: noc01.phx2.fedoraproject.org
    ignore_errors: true
    when: env != 'staging'
  tasks:
  - name: clean dnf metadata
    command: dnf {% for repo in fedora_repos %}--enablerepo {{ repo }} {% endfor %} clean all
  - name: create dnf metadata cache
    command: dnf {% for repo in fedora_repos %}--enablerepo {{ repo }} {% endfor %} makecache
  - name: stop services
    service: name="{{ item }}" state=stopped
    with_items: "{{ koschei_backend_services }}"
  - name: upgrade koschei
    dnf:
      name: "{{ item }}"
      state: latest
      enablerepo: "{{ fedora_repos | join(',') }}"
    register: backend_upgrade
    with_items:
    - koschei-admin
    - koschei-backend
    - koschei-backend-fedora

- name: upgrade koschei frontend
  hosts: "{{ koschei_web_hosts |  default('koschei-web:koschei-web-stg') }}"
  vars_files:
  - /srv/web/infra/ansible/vars/global.yml
  - /srv/private/ansible/vars.yml
  - /srv/web/infra/ansible/vars/{{ ansible_distribution }}.yml
  vars:
    epel_repos:
    - epel
  pre_tasks:
  - name: schedule nagios downtime
    nagios: action=downtime minutes=20 service=host host={{ inventory_hostname_short }}{{ env_suffix }}
    delegate_to: noc01.phx2.fedoraproject.org
    ignore_errors: true
    when: env != 'staging'
  tasks:
  - name: clean yum metadata
    command: yum {% for repo in epel_repos %}--enablerepo {{ repo }} {% endfor %} clean all
  - name: create yum metadata cache
    command: yum {% for repo in epel_repos %}--enablerepo {{ repo }} {% endfor %} makecache
  - name: stop httpd
    service: name="httpd" state=stopped
  - name: upgrade koschei
    yum:
      name: "{{ item }}"
      state: latest
      enablerepo: "{{ epel_repos | join(',') }}"
    register: frontend_upgrade
    with_items:
    - koschei-frontend
    - koschei-frontend-fedora

- name: run DB migration
  hosts: "{{ koschei_backend_hosts |  default('koschei-backend:koschei-backend-stg') }}"
  vars_files:
  - /srv/web/infra/ansible/vars/global.yml
  - /srv/private/ansible/vars.yml
  - /srv/web/infra/ansible/vars/{{ ansible_distribution }}.yml
  tasks:
  - name: run db migration
    become: yes
    become_user: koschei
    command: koschei-admin alembic upgrade head

- name: restart koschei frontend
  hosts: "{{ koschei_web_hosts |  default('koschei-web:koschei-web-stg') }}"
  vars_files:
  - /srv/web/infra/ansible/vars/global.yml
  - /srv/private/ansible/vars.yml
  - /srv/web/infra/ansible/vars/{{ ansible_distribution }}.yml
  handlers:
  - import_tasks: "{{ handlers_path }}/restart_services.yml"
  roles:
  - mod_wsgi
  - koschei/frontend

- name: restart koschei backend
  hosts: "{{ koschei_backend_hosts |  default('koschei-backend:koschei-backend-stg') }}"
  vars_files:
  - /srv/web/infra/ansible/vars/global.yml
  - /srv/private/ansible/vars.yml
  - /srv/web/infra/ansible/vars/{{ ansible_distribution }}.yml
  handlers:
  - import_tasks: "{{ handlers_path }}/restart_services.yml"
  roles:
  - koschei/backend

- name: Check whether backend update happened
  hosts: "{{ koschei_backend_hosts |  default('koschei-backend:koschei-backend-stg') }}"
  tasks:
  - assert:
      that: backend_upgrade.changed

- name: Check whether frontend update happened
  hosts: "{{ koschei_web_hosts |  default('koschei-web:koschei-web-stg') }}"
  tasks:
  - assert:
      that: frontend_upgrade.changed
